package com.oa.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.joneying.common.web.response.Resp;
import com.oa.admin.entity.DocumentFile;
import com.oa.admin.service.IDocumentFileService;
import com.oa.core.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/documentFile")
@Api(description = "文档文件表模块")
public class DocumentFileController extends BaseController {

    private final IDocumentFileService documentFileService;

    @Autowired
    public DocumentFileController(IDocumentFileService documentFileService) {
        this.documentFileService = documentFileService;
    }

    @GetMapping("/list/document/{id}")
    @CommonController
    @Description("根据文档ID查询文件列表")
    @ApiOperation(value = "根据文档ID查询文件列表")
    public Resp<List<DocumentFile>> list(
            @ApiParam(value = "id", required = true) @PathVariable String id,
            @RequestParam(required = false) String fileName,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
            ) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("document_id", id);
        if(StringUtils.isNotBlank(fileName)){
            wrapper.eq("file_name", fileName);
        }
        wrapper.eq("is_deleted", 2);
        Page page = new Page(pageNum, pageSize);
        return new Resp().success(new PageResult(documentFileService.selectPage(page, wrapper).getRecords(), page));
    }


    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<DocumentFile> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(documentFileService.findDocumentFileById(id));
    }


    /**
     * 新增
     *
     * @param documentFile DocumentFile
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "文档文件表对象") @RequestBody(required = false) DocumentFile documentFile) {
        documentFile.setCreateTime(new Date());
        if (documentFileService.insert(documentFile)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param documentFile DocumentFile
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "文档文件表对象") @RequestBody(required = false) DocumentFile documentFile) {
        if (documentFileService.updateById(documentFile)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        DocumentFile documentFile = documentFileService.selectById(id);
        if (documentFile != null && documentFile.getIsDeleted() == 2) {
            documentFile.setIsDeleted(1);
            if (documentFileService.updateById(documentFile)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
